﻿#pragma once

#include <QObject>
#include <QSerialPort>
#include <QDataStream>
#include <QThread>
#include <QDebug>
#include <QMutex>
#include <QTimer>
#include <QFile>


#include "GlobalDefine.h"
#include "DataProcess.h"
#include "SendMsgWorker.h"

class Communicator  : public QObject
{
	Q_OBJECT

public:
	Communicator(QObject *parent = nullptr);
	~Communicator();

	quint16 calculateCRC16(const QByteArray &data);

	//BatteryInfoRom getBatteryInfo();

	
	void writeData(QByteArray data);

	void openSerialPort();
	void closeSerialPort();

	void sendReadBatteryInfoByteArray();
	void sendReadBatteryDetailInfoByteArray();
	void sendReadProtectParamInfoByteArray();
	void sendCHG_CMD_CloseByteArray();
	void sendCHG_CMD_OpenByteArray();
	void sendDSG_CMD_CloseByteArray();
	void sendDSG_CMD_OpenByteArray();
	void sendRESET_CMDByteArray();
	void sendERASEFLASH_CMDByteArray();


	QByteArray getBatteryInfoByteArray();
	QByteArray getBatteryDetailInfoByteArray();
	QByteArray getProtectParamInfoByteArray();
	QByteArray getFactoryParamInfoByteArray();
// 	QByteArray getProtectStationInfoByteArray();
// 	QByteArray getAlarmStationInfoByteArray();

	void updateBMS(QString filePath);
	void sendLengthData(int dataLength,int index);
	void sendUpdateFileData();
	void sendUpdateFileData(int &times,int &num, QFile &file);

	void writeTOV(int data);
	void writeTOVDelay(int data);
	void writeTOVR(int data);
	void writeTOVRDelay(int data);
	void writeOV(int data);
	void writeOVDelay(int data);
	void writeOVR(int data);
	void writeOVRDelay(int data);
	void writeTUV(int data);
	void writeTUVDelay(int data);
	void writeTUVR(int data);
	void writeTUVRDelay(int data);
	void writeUV(int data);
	void writeUVDelay(int data);
	void writeUVR(int data);
	void writeUVRDelay(int data);
	void writeOTD(int data);
	void writeOTDDelay(int data);
	void writeOTDR(int data);
	void writeOTDRDelay(int data);
	void writeOTC(int data);
	void writeOTCDelay(int data);
	void writeOTCR(int data);
	void writeOTCRDelay(int data);
	void writeUTD(int data);
	void writeUTDDelay(int data);
	void writeUTDR(int data);
	void writeUTDRDelay(int data);
	void writeUTC(int data);
	void writeUTCDelay(int data);
	void writeUTCR(int data);
	void writeUTCRDelay(int data);
	void writeOCD1(int data);
	void writeOCD1Delay(int data);
	void writeOCD1R(int data);
	void writeOCD1RDelay(int data);
	void writeOCC(int data);
	void writeOCCDelay(int data);
	void writeOCCR(int data);
	void writeOCCRDelay(int data);
	void writeOCD2(int data);
	void writeOCD2Delay(int data);
	void writeOCD2R(int data);
	void writeOCD2RDelay(int data);
	void writeSC(int data);
	void writeOTMOS(int data);
	void writeOTMOSDelay(int data);
	void writeOTMOSR(int data);
	void writeOTMOSRDelay(int data);

	void writeUART1_ADDR(int data);
	void writeUART1_Bound(int data);
	void writeUART2_ADDR(int data);
	void writeUART2_Bound(int data);
	void writeUART3_ADDR(int data);
	void writeUART3_Bound(int data);
	void writeUART4_ADDR(int data);
	void writeUART4_Bound(int data);
	void writeCAN1_BPS(int data);
	void writeCAN2_BPS(int data);
	void writeCAN3_BPS(int data);
	void writeCAN4_BPS(int data);
	void writePACK_IterationFcc(int data);
	void writeCHG_Voltage(int data);
	void writeCHG_Current(int data);
	void writeREALRC_H(int data);
	void writeREALRC_L(int data);
	void writeOFFECT_I(int data);
	void writeSampRes(int data);
	void writeHOT_MODE(int data);
	void writeHOT_ON_TEMP(int data);
	void writeHOT_OFF_TEMP(int data);
	void writeCHG_DSG_MODE(int data);
	void writeKEY_MODE(int data);
	void writeCD_ForceTime(int data);
	void writeBattery_Type(int data);
	void writeBal_Res(int data);
	void writeBal_Type(int data);
	void writeBal_ONVol(int data);
	void writeBal_Vdiff(int data);
	void writeLowPower_BEEP_EN(int data);


	bool ifCrcCorrect();
signals:
	void sendInitProtectParamInfo(ProtectParamInfo protecParamInfo);
	void sendInitFactoryParamInfo(FactoryParamInfo factoryParamInfo);
	void connectSuccess();
	void updateSuccess();
	void updateError();
	void updateSchedule(int scheduleData);
public slots:
	void setSerialPort();
	void testThread();
	void startCycleThreadTimer();
	//void cycleWork();
	void setLastByteArr(QByteArray arr);

private:
	void init();
	void initSendMsgVec();
	void initCycleRead();

private:
	QSerialPort m_serialPort;
	QByteArray m_lastByteArray;
	QByteArray m_nowByteArr;
	DataProcess *m_processObj;
	QThread *m_processThread;
	QThread *m_cycleThread;
	QMutex m_lock;
	QTimer *m_timer;

	//循环读取工作线程
	SendMsgWorker *m_sendWorker;
	QVector<QByteArray> m_sendMsgVec;

	QString m_configFilePath;
	int m_configFileSize;
};
